package com.jpl.seminar.ui.util;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Vector;

import com.jpl.seminar.ui.module.registration.ModelPropertiesConstant;
import com.vaadin.data.Item;
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.terminal.Resource;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.AbstractSelect.Filtering;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.TextArea;
import com.vaadin.ui.TextField;

public class DemoHelper {

	public static Vector<String> getFormFieldsOrder() {

		// Set the order of the items in the form. 
		Vector<String> order = new Vector<String>();
		order.add(ModelPropertiesConstant.REGISTRATION_COMPANY_FIELD);
		order.add(ModelPropertiesConstant.REGISTRATION_ADDRESS_FIELD);
		order.add(ModelPropertiesConstant.REGISTRATION_NPA_AND_CITY_FIELD);
		order.add(ModelPropertiesConstant.REGISTRATION_COUNTRY_FIELD);
		order.add(ModelPropertiesConstant.REGISTRATION_PHONE_FIELD);
		order.add(ModelPropertiesConstant.REGISTRATION_FAX_FIELD);
		order.add(ModelPropertiesConstant.REGISTRATION_PARTICIPANTS_FIELD);
		return order;
	}

	public static TextField createTextField(String caption, String width, boolean required, String requiredError) {
		TextField textField = new TextField(caption);
		textField.setNullRepresentation("");
		textField.setWidth(width);
		textField.setRequired(required);
		textField.setRequiredError(requiredError);
		textField.setImmediate(true);
		return textField;
	}

	public static TextArea createTextArea(String caption, String width, int rows, boolean required, String requiredError) {
		TextArea textArea = new TextArea(caption);
		textArea.setNullRepresentation("");
		textArea.setWidth(width);
		textArea.setRows(rows);
		textArea.setRequired(required);
		textArea.setRequiredError(requiredError);
		return textArea;
	}

	public static ComboBox getCountryComboBox(String caption, String width, boolean required, String requiredError, Locale locale) {
		ComboBox comboBox = new ComboBox(caption);
		comboBox.setWidth(width);
		comboBox.setContainerDataSource(ExampleUtil.getISO3166Container(locale));

		// Sets the combobox to show a certain property as the item caption
		comboBox.setItemCaptionPropertyId(ISO3166_PROPERTY_NAME);
		comboBox.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_PROPERTY);

		// Sets the icon to use with the items
		comboBox.setItemIconPropertyId(ISO3166_PROPERTY_FLAG);

		// Set the appropriate filtering mode for this example
		comboBox.setFilteringMode(Filtering.FILTERINGMODE_STARTSWITH);
		comboBox.setImmediate(true);

		// Disallow null selections
		comboBox.setNullSelectionAllowed(false);
		comboBox.setNewItemsAllowed(false);
		comboBox.setRequired(required);
		comboBox.setRequiredError(requiredError);
		return comboBox;
	}

	// Handle country combo
	public static final Object ISO3166_PROPERTY_NAME = "name";
	public static final Object ISO3166_PROPERTY_SHORT = "short";
	public static final Object ISO3166_PROPERTY_FLAG = "flag";

	public static Map<String, Locale> countries = new HashMap<String, Locale>();

	static {
		for (Locale locale : Locale.getAvailableLocales()) {
			if (locale.getCountry() != null && !"".equals(locale.getCountry())) {
				countries.put(locale.getCountry(), locale);
			}
		}
	}

	public static IndexedContainer getISO3166Container(Locale locale) {
		IndexedContainer c = new IndexedContainer();
		fillIso3166Container(c, locale);
		return c;
	}

	private static void fillIso3166Container(IndexedContainer container, Locale theLocale) {

		container.addContainerProperty(ISO3166_PROPERTY_NAME, String.class, null);
		container.addContainerProperty(ISO3166_PROPERTY_SHORT, String.class, null);
		container.addContainerProperty(ISO3166_PROPERTY_FLAG, Resource.class, null);
		for (Entry<String, Locale> country : countries.entrySet()) {
			String id = country.getKey();
			String name = country.getValue().getDisplayCountry(theLocale);

			Item item = container.addItem(id);
			item.getItemProperty(ISO3166_PROPERTY_NAME).setValue(name);
			item.getItemProperty(ISO3166_PROPERTY_SHORT).setValue(id);
			item.getItemProperty(ISO3166_PROPERTY_FLAG).setValue(new ThemeResource("./icons/flags/" + id.toLowerCase() + ".gif"));
		}
		container.sort(new Object[] { ISO3166_PROPERTY_NAME }, new boolean[] { true });
	}

}
